package com.itheima.xiancheng.homework.homework6;

import java.util.Random;

public class Take implements Runnable {
    private Cangku ck;

    public Take(Cangku ck) {
        this.ck=ck;
    }

    @Override
    public void run() {

        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (ck){
                Random r=new Random();
                int i = r.nextInt(10) + 1;
                if (ck.isFlag()) {
                    while((ck.getThCount()-i)>=0){
                        ck.take();
                        System.out.println("消费取出1个单位货物，现在仓库剩余" + ck.getThCount());

                    }
                    ck.setFlag(false);
                    ck.notify();

                } else {
                    try {
                        ck.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }
    }
}
